package org.example.zuoye.controller;


import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.zuoye.common.ClientLoginInterceptor;
import org.example.zuoye.entity.Appoint;
import org.example.zuoye.entity.Patient;
import org.example.zuoye.entity.Schedules;
import org.example.zuoye.mapper.SchedulesMapper;
import org.example.zuoye.service.AppointService;
import org.example.zuoye.service.PatientService;
import org.example.zuoye.service.SchedulesService;
import org.example.zuoye.vo.AppointInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/client")
@Slf4j
public class BookController {
    private AppointService appointService;
    @Autowired
    private SchedulesMapper scheduleMapper;

    @Autowired
    public void setAppointService(AppointService AppointService) {
        this.appointService = AppointService;
    }

    @PostMapping("/book")
    @ResponseBody
    public Map<String, Object> book(Integer scheduleId, HttpSession session) {
        Patient patient = (Patient) session.getAttribute(ClientLoginInterceptor.CLIENT_USER_KEY);
        /**
         * 病人模型 给 预约
         */
        Appoint appoint = new Appoint();
        appoint.setPatient(patient);
        /**
         * 放号模型 给 预约
         */
        Schedules schedules = scheduleMapper.selectByPrimaryKey(Long.valueOf(scheduleId));
        appoint.setSchedules(schedules);

        try {
            this.appointService.save(appoint);
            return Map.of("success", true);
        }catch (Exception e){
            return Map.of("error", e.getMessage());
        }
    }

    @PostMapping("/appoint")
    @ResponseBody
    public Map<String, Object> appoint(@RequestBody AppointInfo appoint){
        try {
            log.info("appoint: {}", appoint,appoint.getDate());
            List<AppointInfo> appoints = this.appointService.getByPatId(appoint.getPatId(),appoint.getDate());
            return Map.of("success", true, "data", appoints);
        }catch (Exception e){
            return Map.of("error", e.getMessage());
        }
    }

    /**
     * 取消预约业务
     */
    @PostMapping("/cancelAppoint")
    @ResponseBody
    public Map<String, Object> cancel(@RequestBody AppointInfo appoint){
        try {
            boolean success = this.appointService.updateByAppId(appoint.getAppId());
            return Map.of("success", true);
        }catch (Exception e){
            return Map.of("error", e.getMessage());
        }
    }
}
